const {
    queryLists,
    queryDetail,
    updateModify,
    removeMoment
} = require("../service/moment.service")
const momentService = require("../service/moment.service")

class MomentController {
    async create(ctx, next) {
        // 获取前端传递过来的数据
        const {
            content
        } = ctx.request.body
        // 获取ctx.user中的id值
        const {
            id
        } = ctx.user
        // 将数据存入数据库
        const result = await momentService.create(content, id)
        ctx.body = {
            code: 200,
            message: '创建成功',
            result
        }
    }

    // 查询列表
    async lists(ctx, next) {
        // 获取前端填写的数据
        const {
            offset,
            size
        } = ctx.request.body

        const result = await queryLists(size, offset)
        ctx.body = {
            code: 200,
            message: '请求成功',
            result
        }
    }

    // 查询详情
    async detail(ctx, next) {
        const {
            momentId
        } = ctx.params
        const result = await queryDetail(momentId)
        ctx.body = {
            code: 200,
            message: '请求成功',
            result
        }
    }

    // 修改内容
    async modify(ctx, next) {
        const {
            momentId
        } = ctx.params
        const {
            content
        } = ctx.request.body
        const result = await updateModify(content, momentId)
        ctx.body = {
            code: 200,
            message: '修改成功',
            result
        }
    }

    // 删除动态
    async removeMoment(ctx,next) {
        const { momentId } = ctx.params
        const result = await removeMoment(momentId)
        try {
            ctx.body = {
                code: 200,
                message:'删除成功',
                result
            }
        } catch (error) {
            console.log(error);
        }
    }
}

module.exports = new MomentController()